package com.wenhao.forkjoin;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

/**
 * @author Wenhao Tong
 * @create 2021-08-20 18:31
 */
public class ForkJoinTest {
    @Test
    public void test1() {
        Long start = System.currentTimeMillis();
        Long sum = 0L;
        for (Long i = 1L;i <= 1000000000L;i++) {
            sum += i;
        }
        System.out.println(sum);
        Long end = System.currentTimeMillis();
        // 9584
        System.out.println(end - start);
    }

    @Test
    public void test2() throws ExecutionException, InterruptedException {
        Long start = System.currentTimeMillis();

        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask task = new ForkJoinDemo(1L, 1000000000L);
        ForkJoinTask submit = forkJoinPool.submit(task);
        System.out.println(submit.get());

        Long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    @Test
    public void test3() {
        Long start = System.currentTimeMillis();
        LongStream.rangeClosed(1L,1000000000L).parallel().reduce(0,Long::sum);
        Long end = System.currentTimeMillis();
        System.out.println(end - start); // 105
    }
}
